using System;
using System.Collections;
using System.Reflection;

namespace Utiles
{
	/// <summary>
	/// Summary description for GenericComparer.
	/// </summary>
	public class GenericComparer : IComparer
	{
		private string sortColumn;
		private bool orderDirection;
	
		public GenericComparer(string sortColumn,bool orderDirection)
		{
			this.sortColumn = sortColumn;
			this.orderDirection = orderDirection;
		}
		public int Compare(object x, object y)
		{
			
			object valueOfX = null;
			object valueOfY = null;
			PropertyInfo pproperty;


			// X
			pproperty = x.GetType().GetProperty(this.sortColumn);			
			valueOfX = pproperty.GetValue(x,null);
			

			//Y
			pproperty = y.GetType().GetProperty(this.sortColumn);
			valueOfY = pproperty.GetValue(y,null);
			

			if (valueOfX != null && valueOfY != null && this.orderDirection)
			{
				return ((IComparable) valueOfX).CompareTo(valueOfY);

			}
			else if (valueOfX != null && valueOfY != null && !this.orderDirection)
			{
				return ((IComparable) valueOfY).CompareTo(valueOfX);	
			}
			else
			{
				return 0;	
			}
		}
	}
}
